﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;


namespace NullSuicide 
{
    class Players
    {
        private List<Player> _ListOfPlayers;
        
        public Players()
        {
            _ListOfPlayers = new List<Player>();
        }

        public Player this[int index]
        {
            get { return _ListOfPlayers[index]; }
            set { _ListOfPlayers[index] = value; }
        }

        public List<Player> ListOfPlayers
        {
            get { return _ListOfPlayers; }
        }

        public Player getPlayer(int playerID)
        {
            return _ListOfPlayers.Find(p => p.ID.Equals(playerID));
        }

        public void sortlist()
        {
            _ListOfPlayers.Sort();
        }

        public void DeletePlayer(int playerID)
        {
            Suicide(playerID, true);
            _ListOfPlayers.Remove(getPlayer(playerID));
        }

        public Player AddPlayer(string name)
        {
            return AddPlayer(-1, name, this._ListOfPlayers.Count + 1, false);
        }
        public Player AddPlayer(string name, bool inRaid)
        {
            return AddPlayer(-1, name, this._ListOfPlayers.Count + 1, inRaid);
        }
        public Player AddPlayer(string name, int possition, bool inRaid)
        {
            return AddPlayer(-1, name, possition, inRaid);
        }

        public Player AddPlayer(int id, string name, int possition)
        {
            Player newPlayer = new Player(id, name, possition);
            _ListOfPlayers.Add(newPlayer);
            return newPlayer;
        }

        public Player AddPlayer(int ID, string name, int possition, bool inRaid)
        {
            Player newPlayer = new Player(ID, name, possition, inRaid);
            _ListOfPlayers.Add(newPlayer);
            return newPlayer;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="playerID"></param>
        /// <param name="suicideBeforeDelete">Use Suicide to move the player to the bottom of the list for easy delete</param>
        public void Suicide(int playerID, bool suicideBeforeDelete)
        {
            _ListOfPlayers.Sort();

            Player suicideplayer = getPlayer(playerID);
            int lastplayerpossition = suicideplayer.Position;

            foreach (Player player in _ListOfPlayers)
            {
                int temppossition = suicideplayer.Position;

                if ((player.InRaid || suicideBeforeDelete) && !player.Equals(suicideplayer) && player.Position > suicideplayer.Position)
                {
                    temppossition = player.Position;
                    player.Position = lastplayerpossition;
                    lastplayerpossition = temppossition;
                }

                suicideplayer.Position = temppossition;
            }

        }

        public void chanceInRaid(int playerID, bool inRaid)
        {
            Player player = getPlayer(playerID);
            player.InRaid = inRaid;
        }
    }
}
